package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.activity.BaseActivity;
import com.imay.live.adapter.SystemNoticeDetailAdapter;
import com.imay.live.helper.SystemNoticeHelper;
import com.imay.live.model.SystemNoticeModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.view.refresh.RefreshView;
import com.imay.live.R;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

public class SystemNoticeDetailActivity extends BaseActivity implements ProtoCallBack, BaseQuickAdapter.OnItemClickListener {

    public static final String TAG_CONTENT_TYPE = "tag_content_type";
    private int contentType;
    private int count = 20;
    private long maxId;
    private String loadSystemNoticeDetailCommand;
    private RecyclerView rcv_system_notice_detail;
    private List<SystemNoticeModel> systemNoticeModels;
    private SystemNoticeDetailAdapter adapter;
    private TwinklingRefreshLayout system_notice_trl_refresh;

    public static void start(Context context, int contentType) {
        Intent starter = new Intent(context, SystemNoticeDetailActivity.class);
        starter.putExtra(TAG_CONTENT_TYPE, contentType);
        context.startActivity(starter);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_system_notice_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        contentType = getIntent().getIntExtra(TAG_CONTENT_TYPE, 0);
        setTitle(getString(SystemNoticeHelper.getTextByType(contentType)));
        rcv_system_notice_detail = $(R.id.rcv_system_notice_detail);
        rcv_system_notice_detail.setLayoutManager(new LinearLayoutManager(this));
        system_notice_trl_refresh = $(R.id.system_notice_trl_refresh);
        RefreshView headerView = new RefreshView(this);
        system_notice_trl_refresh.setHeaderView(headerView);
        system_notice_trl_refresh.setBottomView(new LoadingView(this));
        system_notice_trl_refresh.setEnableLoadmore(true);
        system_notice_trl_refresh.setEnableOverScroll(false);
        system_notice_trl_refresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                maxId = 0;
                loadSystemNoticeDetail();
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                loadMore();
            }
        });
        systemNoticeModels = new ArrayList<>();
        loadSystemNoticeDetail();


    }

    private void loadSystemNoticeDetail() {
        loadSystemNoticeDetailCommand = ProtoRequestHelper.task(ProtoHttpFactory.getMailRecord(contentType, count, maxId)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        refreshFinish();
        if (command.equals(loadSystemNoticeDetailCommand)) {
            Result<List<SystemNoticeModel>> listResult = JSON.parseObject(data, new TypeReference<Result<List<SystemNoticeModel>>>() {
            }.getType());
            if (listResult.result == CodeSuccess) {
                if (maxId==0){
                    systemNoticeModels.clear();
                }
                systemNoticeModels.addAll(listResult.data);
                bindAdapter();
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        refreshFinish();

    }

    private void bindAdapter() {
        if (adapter == null) {
            adapter = new SystemNoticeDetailAdapter(systemNoticeModels);
            rcv_system_notice_detail.setAdapter(adapter);
            adapter.setOnItemClickListener(this);
        } else {
            adapter.setNewData(systemNoticeModels);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        SystemNoticeModel model = systemNoticeModels.get(position);
        if (model.getContentType() == 8 && model.getContentId() == 1) {
            ProfileEditActivity.start(this);
        } else if (model.getContentType() == 3 && model.getContentId() == 9) {
            PermisionCheckActivity.start(this);
        } else if (model.getContentType() == 2 && model.getContentId() == 2) {
            PermisionCheckActivity.start(this);

        }

    }

    private void loadMore() {
        int size = systemNoticeModels.size();
        if (size > 0) {
            maxId = systemNoticeModels.get(size - 1).getId();
        }
        loadSystemNoticeDetail();

    }
    /**
     * 完成刷新
     */
    public void refreshFinish() {
        if (system_notice_trl_refresh != null) {
            if (maxId==0){
                system_notice_trl_refresh.finishRefreshing();
            }else{
                system_notice_trl_refresh.finishLoadmore();
            }
        }
    }
}
